package com.ochnas.volunteersignup.ui;

import static com.ochnas.volunteersignup.ui.SharedConstants.PHONE_NUMBER_PATTERN;

import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.UserService;

public class ManageAccountController {
	private ApplicationService applicationService;
	private UserService userService;
	private User user;

	public ManageAccountController() {
		super();
		user = new User();
	}

	public String manageAccount() {

		String userId = applicationService.getRequestParameter("userId");
		try {
			user = userService.findUserAccount(Long.parseLong(userId));
		} catch (NumberFormatException e) {
			return null;
		}

		return "manageAccount";
	}

	public String editUserAccount() {
		return "editUserAccount";
	}

	public String cancel() {
		user = userService.findUserAccount(user.getId());
		return "manageAccount";
	}

	public String updateAccount() {

		boolean isValid = true;

		// validate if first name field is empty
		if (user.getFirstName() == null || "".equals(user.getFirstName())) {
			applicationService.addErrorMessage("editUserAccountForm:firstName",
					"Please enter first name.");
			isValid = false;
		}
		// validate if last name field is empty
		if (user.getLastName() == null || "".equals(user.getLastName())) {
			applicationService.addErrorMessage("editUserAccountForm:lastName",
					"Please enter last name.");
			isValid = false;
		}

		// validate if phone number field is empty
		if (user.getContactPhoneNumber() == null
				|| "".equals(user.getContactPhoneNumber())) {
			applicationService.addErrorMessage(
					"editUserAccountForm:contactPhoneNumber",
					"Please enter phone number.");
			isValid = false;
		} else {
			if (!PHONE_NUMBER_PATTERN.matcher(user.getContactPhoneNumber()).matches()) {
				applicationService.addErrorMessage(
						"editUserAccountForm:contactPhoneNumber",
						"Phone Number Must Be Entered As: 7632211234 ");
				isValid = false;
			}
		}

		if (isValid) {
			try {
				userService.updateUserAccount(user.getId(),
						user.getFirstName(), user.getLastName(), user
								.getContactPhoneNumber());
			} catch (Exception e) {
				applicationService.addErrorMessage(
						"editUserAccountForm:summary", e.getMessage());
				return null;
			}
		} else {
			return null;
		}

		applicationService.addErrorMessage("manageAccountForm:summary",
				"Your account has been successfully updated.");
		return "manageAccount";

	} // end of edit user account method

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
